////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package plantool.display.splash {
	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	import flash.display.NativeWindow;
	import flash.display.NativeWindowInitOptions;
	import flash.display.NativeWindowSystemChrome;
	import flash.display.Screen;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.text.TextField;
	
	import kiwi.display.Image;
	import kiwi.events.CallbackEvent;
	import kiwi.system.Base;
	import kiwi.utils.Validation;
	
	import plantool.helpers.TextFieldHelper;
	import plantool.core.StatusManager;
	
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	public class SplashScreen extends NativeWindow {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		private var _splashImage:Image				= null;
		private var _statusTf:TextField				= null;
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		private function get base():Base {
			return Base.getInstance();
		}
		private function get statusManager():StatusManager {
			return StatusManager.getInstance();
		}
		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function SplashScreen() {
			//create initoptions for the splash-screen
			var initOptions:NativeWindowInitOptions	= new NativeWindowInitOptions();
			initOptions.systemChrome				= NativeWindowSystemChrome.NONE;
			
			//create the splash screen
			super(initOptions);
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		private function onChange_status(e:Event):void {
			_statusTf.text		= statusManager.status;
		}
		
		private function onComplete_loadSplashImage(e:CallbackEvent):void {
			//store the splash image
			_splashImage	= e.currentTarget as Image;
			
			//set the splash-screen min and max size
			this.minSize	= new Point(_splashImage.width, _splashImage.height);
			this.maxSize	= new Point(_splashImage.width, _splashImage.height);
			
			//set stage properties
			this.stage.align		= StageAlign.TOP_LEFT;
			this.stage.scaleMode	= StageScaleMode.NO_SCALE;
			
			//add the splashimage
			this.stage.addChild(_splashImage);
			
			//create the textfield
			_statusTf 		= TextFieldHelper.createInfoTf();
			_statusTf.x		= 10;
			_statusTf.y		= _splashImage.height - _statusTf.height - 10;
			_statusTf.width	= _splashImage.width - 20;
			this.stage.addChild(_statusTf);
			
			//position outself in the center
			this.x		= (Screen.mainScreen.bounds.width - _splashImage.width) / 2;
			this.y		= (Screen.mainScreen.bounds.height - _splashImage.height) / 2;
			
			//listen to status change
			statusManager.addEventListener(Event.CHANGE		, onChange_status);
			statusManager.status = "Starting Application...";
			
			//show the splash screen
			super.activate();
		}
		
		public override function activate():void {
			//load the splash image
			new Image(base.config.BASEURL + "data/images/splash.png"		, onComplete_loadSplashImage);
		}
		
		public override function close():void {
			//check if the splash-image exists, then dispose it
			if (Validation.isSet(_splashImage)) {
				_splashImage.dispose();
				_splashImage = null;
			}
			
			//check status textfield
			if (Validation.isSet(_statusTf)) {
				//remove status textfield from stage
				if (this.stage.contains(_statusTf)) {
					this.stage.removeChild(_statusTf);
				}
				
				//clean up status tf var
				_statusTf	= null;
			}
			
			//remove status change event
			statusManager.removeEventListener(Event.CHANGE		, onChange_status);
			
			//call super close
			super.close();
		}
	}
}